package cn.dengta.webapp.user.entity;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Random;

import cn.dengta.common.security.AES;
import cn.dengta.common.util.Formats;
import cn.dengta.common.util.SpringUtil;
import cn.dengta.context.auth.DefaultAutoLoginHandler;
import me.codeplayer.util.*;

/**
 * 用户核心字段信息自动生成器
 */
public class UserFieldAutoGenerator<T extends MemberContent> {

	static final String PASSWORD_SALT = "dtCyI.Pzz——YfWF_kR/lHrjRjD5KQbaOw=";

	public static String encryptPassword(String username, String password, Date addTime) {
		Assert.notEmpty(username, "username is required");
		Assert.notNull(addTime, "addTime is required");
		String str = username + '@' + password + '@' + PASSWORD_SALT + '@' + Formats.formatDate_D(addTime);
		return Encrypter.bytes2Hex(Encrypter.getMessageDigest("SHA-224").digest(str.getBytes(StandardCharsets.UTF_8)));
	}

	public static String encryptPassword(String password, Date addTime) {
		Assert.notNull(addTime, "addTime is required");
		String str = password + '@' + PASSWORD_SALT + '@' + Formats.formatDate_D(addTime);
		return Encrypter.bytes2Hex(Encrypter.getMessageDigest("SHA-224").digest(str.getBytes(StandardCharsets.UTF_8)));
	}

	public static <T> String makeInviteCode(T input) {
		// 不能是 11 位的数字
		final String charSource = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		final int len = charSource.length();
		final Random random = new Random();
		final int total = random.nextInt(6) + 3;
		final char[] chars = new char[total];
		for (int i = 0; i < total; i++) {
			int index = random.nextInt(len);
			if (i == 0) {
				while (index < 10) {
					index = random.nextInt(len);
				}
			}
			chars[i] = charSource.charAt(index);
		}
		return new String(chars);
	}

	public static <T extends MemberContent> void setInviteCode(T input) {
		if (StringUtil.isEmpty(input.getInviteCode())) {
			input.setInviteCode(makeInviteCode(input));
		}
	}

	public static <T extends MemberContent> String makeUsername(T input) {
		return makeUsername(RandomUtil.getIntString(11), input.getAddTime().getTime());
	}

	public static String makeUsername(String phone, long addTime) {
		final int radix = Character.MAX_RADIX;
		BigInteger bi = new BigInteger(phone + new EasyDate(addTime).toShortString());
		return "t_" + bi.toString(radix);
	}

	public static <T extends MemberContent> void setUsername(T input) {
		if (StringUtil.isEmpty(input.getUsername())) {
			input.setUsername(makeUsername(input));
		}
	}

	/**
	 * 创建密码
	 */
	public static String createPassword(String prefix) {
		final Random random = new Random();
		final int size = random.nextInt(5) + 6;
		final char[] chars = new char[size];
		int index = 0;
		while (size > index) {
			int n = random.nextInt(10 + 26 + 26); // 0~51
			char ch;
			if (n < 10) {
				ch = (char) ('0' + n);
			} else if (n < 36) {
				ch = (char) ('a' - 10 + n);
			} else {
				ch = (char) ('A' - 36 + n);
			}
			if (ch == 'o' || ch == 'O' || ch == 'l') {
				// 这几个字符肉眼容易看错，因此跳过这几个字符
				continue;
			}
			chars[index++] = ch;
		}
		return StringUtil.toString(prefix) + new String(chars);
	}

	static AES aes;

	/** 用于证件信息加密 的 AES 实例类 */
	public static AES certInfoAes() {
		if (aes == null) {
			aes = SpringUtil.getBean(DefaultAutoLoginHandler.class).getAes();
		}
		return aes;
	}

}
